package com.shop.dao.impl;

import com.shop.dao.GoodTypeDao;
import com.shop.entity.GoodType;
import com.shop.entity.ShowPageT;
import com.shop.tools.JdbcTool;

import java.sql.SQLException;
import java.util.List;

/**
 * com.shop.dao.impl文档说明
 *
 * @author Angluzha
 * @version 2022/9/14
 */
public class GoodTypeDaoImpl implements GoodTypeDao{
    @Override
    public GoodType selectOne(GoodType goodType) throws SQLException {
        String sql = "select * from goodtype where id = ?";
        return JdbcTool.selectOne(GoodType.class,sql,goodType.getId());
    }

    @Override
    public boolean update(GoodType goodType) {
        String sql = "update goodtype set typename=? ,icon=? ,pid=? where id=?";
        return JdbcTool.write(sql,goodType.getTypename(),goodType.getIcon(),goodType.getPid(),goodType.getId())>0;
    }

    @Override
    public boolean insert(GoodType goodType) {
        String sql = "insert into goodtype(typename,icon,pid) values(?,?,?)";
        return JdbcTool.write(sql,goodType.getTypename(),goodType.getIcon(),goodType.getPid())>0;
    }

    @Override
    public boolean deleteById(Integer id) {
        String sql = "delete from goodtype where id = ?";
        return JdbcTool.write(sql,id)>0;
    }

    @Override
    public List<GoodType> selectAll() throws SQLException {
        String sql = "select * from goodtype";
        return JdbcTool.selectList(GoodType.class,sql);
    }

    @Override
    public ShowPageT<GoodType> selectPage(ShowPageT<GoodType> page) throws SQLException {
        String sql = "SELECT * FROM goodtype LIMIT ?,?";
        String sql2 = "select count(1) from goodtype";
        page.setCount(JdbcTool.selectResult(Integer.class,sql2));
        page.setData(JdbcTool.selectList(GoodType.class,sql,page.getSize()*(page.getPage()-1),page.getSize()));
        return page;
    }

    @Override
    public int delCheck(List<Integer> list) {
        StringBuffer sql = new StringBuffer("delete from goodstype where id in(");
        list.forEach(e->{
            sql.append("?,");
        });
        sql.deleteCharAt(sql.length()-1).append(")");
        return JdbcTool.write(String.valueOf(sql),list.toArray());
    }
}
